#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>

int *a;
int q;
int append(int a[],int o)
	{
	int i;
	printf("Enter the element you want to append:");
	scanf("%d",&o);
	q=q+1;
	a=realloc(a,sizeof(int)*q);
	a[q-1]=o;
	
	printf("Final array is:\n");
		for (i=0;i<q;i++)
		{
		printf("%d ",a[i]);
		}
	}


int index(int a[],int q)
{
int t,i;
printf("Enter the number whose position you want to know");
scanf("%d",&t);
for (i=0;i<q;i++)
{
if(a[i]==t)
{printf("The position is %d",i+1);
break;
}
}
}

int extend(int a[],int q)
{
int i,k;
printf("Enter the number of elements you want to extend:");
scanf("%d",&k);
for(i=q;i<q+k;i++)
{
printf("Enter the element:");
scanf("%d",&a[i]);
}
printf("Array is:");
for (i=0;i<q+y;i++)
{
printf("%d",a[i]);
}
}

int insert(int a[],int q)
{
	int j,p,i;
	printf("Enter the position where you want to insert");
	scanf("%d",&j);
	printf("Enter the element:");
	scanf("%d",&p);
	q=q+1;
	a=realloc(a,sizeof(int)*q);
	for (i=q;i>j-1;i--)
	{
	a[i]=a[i-1];
	}
	a[j]=p;
	printf("Array is:");
	for (i=0;i<q;i++)
	printf("%d ",a[i]);
}


int count(int a[],int q)
{
	int d,count=0,i;
	printf("Enter the number which you want to count:");
	scanf("%d",&d);
	for (i=0;i<q;i++)
	{
	if (a[i]==d)
	count+=1;
	}
	printf("Count=%d\n",count);
	
}


int remove(int a[],int q)
{
int j,k,i;
printf("Enter the element you want to remove:");
scanf("%d",&k);
	for(i=0;i<q;i++)
	{
		if(a[i]==k)
		{
			break;
		
		}
	}
q=q-1;
a=realloc(a,sizeof(int)*q);
for(j=i;j<=q-1;j++)
{
	a[j]=a[j+1];
}

printf("Final Array is:");
for (i=0;i<q;i++)
	{
	printf("%d ",a[i]);
	}
}

int pop(int a[],int q)
{
	int pos,j,i,l;
	printf("Enter the position of the element to pop:");
	scanf("%d",&pos);
	q=q-1;
	a=realloc(a,sizeof(int)*q);
l=a[pos]
for(j=pos;j<=q-1;j++)
{
	a[j]=a[j+1];
}
printf("\n\item popped is %d \n",l);
printf("Final Array is:");
for (i=0;i<q;i++)
{
printf("%d ",a[i]);
}
}
	


int reverse(int a[],int q)
{
int j;
printf("final array is:");
for (j=q-1;j>=0;j--)
{
printf("%d ",a[j]);
}
}



int sort(int a[],int q)
{
	int i,j,k,c;
	for(i=0;i<=q-1;i++)
	{
		for(j=0;j<=q;j++)
		{
			if(a[j]<a[i])
			{
				c=a[i];
				a[i]=a[j];
				a[j]=c;
			}
		}
	}
		
	for(k=0;k<=q-1;k++)
	{
		printf("%d ",a[k]);
	}
}



void main()
{
int i,n;
printf("Enter the size of array:");
scanf("%d",&q);
a=malloc(sizeof(int)*q);

for (i=0;i<q;i++)
{
printf("Enter the element:");
scanf("%d",&a[i]);
}
printf("List of 0perations:-\n");
printf("--------------------------\n\t1.append\n\t2.index\n\t3.extend\n\t4.insert\n\t5.pop\n\t6.count\n\tremove\n\treverse\n\tsort\nEnter the coice (0 to exit) \n");

scanf("%d",&n);
while(n!=0){switch(n){
case 1: append(a,q);
	break;
case 2: inde(a,q);
	break;
case 3: extend(a,q);
	break;
case 4:	insert(a,q);
	break;
case 5: pop(a,q);
	break;
case 6: count(a,q);
	break;
case 7: remov(a,q);
	break;
case 8: reverse(a,q);
	break;
case 9: sort(a,q);
	break;
case 0:exit(1);}
}}






